///////List implementation using Arrays/////////////////////

#include <stdio.h>
#include <stdlib.h>

int count=0 , count2=0 , flag=0;       // global variables...count is size of list 1 and count 2 is size of list 2..
	

//*************************************************************************************************************//

int * create()                         // creates a new list.. takes no arguments but returns the list..
  
{
int *A,i,sizeA;


printf("Enter the no. of elements you want to enter : \n");
scanf("%d",&sizeA);
A=(int *) malloc(sizeA*sizeof(int));                              // size is entered by the user..

for (i=0;i<sizeA;i++)
	{
	scanf("%d,",A+i);
	}
	if( count==0)
		count=sizeA;
	else
		{
			count2=sizeA;
			flag=1;
		}
return A;
}

//*************************************************************************************************************//

void show(int *A)                      //prints the list (no arguments taken)
{int i;
printf("The list is : \n");

printf("[");
if(flag==0)                                                 //checks if previously list 1 was made...
	{
		for (i=0;i<count;i++)
		{
		printf("%d  ",*(A+i));
		}
	}
	
else
	{
		for (i=0;i<count2;i++)
		{
		printf("%d  ",*(A+i));
		}
		flag=0;
	}

printf("] \n");
}

//*************************************************************************************************************//

int * rev (int * A)                //reverses the list and takes the argument of the previous list..and returns the reversed list..
{
int i,*C;
C=(int *) malloc(count*sizeof(int));
	for (i=0;i<count;i++)
		{
		*(C+i)=*(A+count-i-1);
		}
	
A=C;

printf("The reversed list is : \n");

printf("[");

	for (i=0;i<count;i++)
		{
		printf("%d  ",*(A+i));
		}

printf("] \n");
return A;
}


//*************************************************************************************************************//

void countx(int *A)     // counts the no. of x's in the list where x is entered by the user.
{
int i,counter,x;
counter=0;
printf("Enter the no. that you want to count : \n");
scanf("%d",&x);
	for (i=0;i<count;i++)

		{
		if(*(A+i)==x)
			{
			counter=counter+1;
			}	
		}
printf("%d appears %d times in the list \n",x,counter);

}

//*************************************************************************************************************//

void index_z(int *A)              //  finds the index of the value entered by user..
{
int i,z,k;
k=0;
printf("Enter the value you want to find the index of : \n");
scanf("%d",&z);
	for (i=0;i<count;i++)
		{
		if(*(A+i)==z)
			{
				k=i+1;
				break;
			}
		}
	if(k==0)
		printf("Value not found in the list \n");
	else
		printf("Value found at position %d \n",k);
	

}

//*************************************************************************************************************//

int * appendx(int *A)        // appends the list with the no. that the user wants to add..
{
int y;
printf("Enter the no. that you want to append : \n");
scanf("%d",&y);

A=realloc(A,(count+1)*sizeof(int));
	*(A+count)=y;
	count=count+1;
return A;

}


//*************************************************************************************************************//


int * extend(int *A)       //extends the initial list with the second list that the user enters..takes the argument of the 1stlist
				
{int i,*B;
	B=create();        //Creating second list..
	show(B);
	A=realloc(A,(count+count2)*sizeof(int)); 
	for (i=0;i<count2;i++)
		{
			*(A+count+i)=*(B+i);
		}
	count=count+count2;
return A;

}


//*************************************************************************************************************//

int * remove_x(int *A)         //removes the entered element ..

{int i,z,k;
k=0;
printf("Enter the value you want to remove : \n");
scanf("%d",&z);
	for (i=0;i<count;i++)
		{
		if(*(A+i)==z)
			{
				k=i+1;
				break;
			}
		}
		
	for (i=k;i<count;i++)
		{
		
		*(A+i-1)=*(A+i);
		
		}
	count=count-1;
	if(k==0)
		printf("Value not found in the list \n");
	else
		printf("Value removed \n");
			
	
return A;
}


//*************************************************************************************************************//

int * pop(int *A)         //pops the element at the given index no...

{int i,z,k;
k=0;
printf("Enter the index value you want to pop : \n");
scanf("%d",&z);
	if(z<count)
	{
		for (i=0;i<count;i++)
			{
			if(i==z)
				{
					printf("You popped %d from the list !!",*(A+z+1));
				}
			}
		
		for (i=z;i<count;i++)
			{
			
			*(A+i-1)=*(A+i);
			
			}
		count=count-1;
		printf("Value removed \n");
	}
	else printf("Index out of range !! \n");

			
	
return A;
}


//*************************************************************************************************************//

void length_x()   // Gives the length of list
{

printf("The length of the list is : %d \n",count);

}

int *sort(int *A)          // Sorts the list
{
	int large,i,j;
	for (i=0; i<count; i++)
		{
			large = i;
			for (j=i; j<count; j++)
			{
				if(*(A+large)>*(A+j))
				{	large = j; }
			}
		int temp = *(A+i);
		*(A+i) = *(A+large);
		*(A+large) = temp;
		}
		
return A;
}


//*************************************************************************************************************//

void slice(int *A)              //slices the list depending upon the min and max value entered by user..
{
int a,b,i;

printf("Enter the starting and ending point values : \n");
scanf("%d,%d",&a,&b);

if(a>0 && b<(count+1))
{
printf("The sliced list is : \n");
printf("[");

	{
		for (i=(a-1);i<b;i++)
		{
		printf("%d  ",*(A+i));
		}
	}
printf("]");
}

else printf("Index values invalid!!");
}


//*************************************************************************************************************//


void main()   // Main Function
{
int *B,opt;

printf("This is Python's list data-type implementation using Dynamic Arrays : \n\n\n\n");
B=create();


do{

printf("________________________________________________________________\n");

printf("The options available are : \n");

printf("1. Display the list \n");

printf("2. Revesre the list \n");

printf("3. Append the list \n");

printf("4. Extend a list with a 2nd list \n");

printf("5. Count the no. of elements in the list \n");

printf("6. Find the index of an element \n");

printf("7. Count the no. of occurences of a value in the list \n");

printf("8. Remove an element from the list \n");

printf("9. Pop an element at a given index from the list \n");

printf("10. Sort the list \n");

printf("11. Slice the list \n");

printf("12. Exit \n");

printf("________________________________________________________________\n");

printf("Enter your choice no. : \n");

scanf("%d",&opt);

switch(opt)
{
case 1 : show(B);break;
case 2 : B=rev(B);break;
case 3 : B=appendx(B);show(B);break;
case 4 : B=extend(B);show(B);break;
case 5 : length_x();break;
case 6 : index_z(B);break;
case 7 : countx(B);break;
case 8 : B=remove_x(B);show(B);break;
case 9 : B=pop(B);show(B);break;
case 10 : B=sort(B);show(B);break;
case 11 : slice(B);break;
case 12 : printf("Exiting.. !!");break;
default : printf("Invalid choice !!Please enter again. \n");break; 
}
}while (opt!=12);
}


//End of code
